package com.cskaoyan.day24.tcp;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Serve {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        Socket socket = serverSocket.accept();
        InputStream in = socket.getInputStream();

        DataInputStream dataInputStream = new DataInputStream(in);
        long strLen = dataInputStream.readLong();
        byte[] bytes = new byte[(int) strLen];
        in.read(bytes);
        System.out.println(new String(bytes, 0, (int) strLen));
        System.out.println("输入字符串读取完成");


        BufferedReader in1 = new BufferedReader(new InputStreamReader(in));
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("zserve/b.txt"));
        String str1;
        System.out.println("开始接收文本文件并且输入到控制台：");
        while ((str1 = in1.readLine()) != null) {
            if (str1.contains("end")) {
                break;
            }
            System.out.println(str1);
        }
        System.out.println("接收完成！");


        while ((str1 = in1.readLine()) != null) {
            if (str1.contains("end")) {
                break;
            }
            out.write(str1);
            out.write('\n');
            out.flush();

        }

        out.close();
        socket.close();
        serverSocket.close();

    }
}
